package com.pac.doctor.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.doctor.bean.DoctorBean;
import com.pac.doctor.dao.DoctorDao;
import com.pac.doctor.service.DoctorService;

/**
 * 医生Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月2日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "doctorService")
public class DoctorServiceImpl implements DoctorService {

	private static Logger logger = Logger.getLogger(DoctorServiceImpl.class);

	@Autowired
	private DoctorDao doctorDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addDoctor(DoctorBean doctorBean) {

		if (null == doctorBean) {
			logger.error("添加医生信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(doctorBean.getUserId())) {
			logger.error("添加医生信息出错，用户ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "用户ID为空");
		}

		if (StringTools.isNull(doctorBean.getHospitalId())) {
			logger.error("添加医生信息出错，医院ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医院ID为空");

		}

		return doctorDao.addDoctor(doctorBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateDoctor(DoctorBean doctorBean) {

		if (null == doctorBean) {
			logger.error("修改用户信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(doctorBean.getId())) {
			logger.error("修改用户信息出错，医生ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医生ID为空");
		}

		return doctorDao.updateDoctor(doctorBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delDoctor(DoctorBean doctorBean) {
		if (null == doctorBean) {
			logger.error("删除医生出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(doctorBean.getId())) {
			logger.error("删除医生出错，医生ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医生ID为空");
		}
		return doctorDao.delDoctor(doctorBean);
	}

	@Override
	public List<DoctorBean> queryDoctor(DoctorBean doctorBean) {

		List<DoctorBean> doctors = doctorDao.queryDoctor(doctorBean);

		return doctors;
	}

	@Override
	public DoctorBean findDoctor(DoctorBean doctorBean) {
		if (null == doctorBean) {
			return null;
		}

		List<DoctorBean> doctors = doctorDao.queryDoctor(doctorBean);
		if (null != doctors && doctors.size() > 0) {
			return doctors.get(0);
		}

		return null;
	}
}
